var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
const cors = require('cors');

var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');

var app = express();

app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use(cors());
app.use('/', indexRouter);
app.use('/users', usersRouter);

// 封装错误处理函数
app.use((req, res, next) => {
  res.cc = function (err, status = 1, data = {} ) {
    res.send({
      status, data, message: err instanceof Error ? err.message : err
    })
  }
  next();
})

module.exports = app;
